import { addMsbdList, editMsbdList, getMsbdList, deleteMsbdList } from '../../api/msbd';

const state = {
    msbdList: [],
    classifyList: {},
    total: 0
}

const mutations = {
    SET_MSBD_LIST(state, list) {
        state.msbdList = list.map(v => {
            return { ...v, date: v.date.split(" ")[0] }
        })
    },
    SET_CLASSIFY_LIST(state, list) {
        state.classifyList = list
    },
    SET_TOTAL(state, total) {
        state.total = total
    }
}

const actions = {
    async renderMsbdList({ commit }, data) {
        let r = await getMsbdList(data)
        commit("SET_MSBD_LIST", r.data)
        commit("SET_CLASSIFY_LIST", r.classify)
        commit("SET_TOTAL", r.total)
    },

    async addMsbdData({ dispatch }, form) {
        await addMsbdList(form)
        dispatch("renderMsbdList")
    },

    async editMsbdData({ dispatch }, form) {
        await editMsbdList(form)
        dispatch("renderMsbdList")
    },

    async deleteMsbdData({ dispatch }, id) {
        let r = await deleteMsbdList(id)
        dispatch("renderMsbdList")
    }
}

const getters = {

}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}